/**
 * mosaic
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2010. 01. 06
*/
package com.cmiscm.utils.color
{
	import flash.display.BitmapData;
	import flash.geom.Rectangle;
	
	/**
	 * Mosaic.
     * 
     */
	public class Mosaic
	{
		/**
		 * draw mosaic
		 * @param $bit BitmapData
		 * @param $size Pixel size
		 * @return 
		 * 
		 * @example
     	 * <listing> 
     	 * package {
         *    import com.cmiscm.utils.color.Mosaic;
         *    import flash.display.Bitmap;
         *    import flash.display.BitmapData;
         *    import flash.display.Sprite;
         *    import flash.events.Event;
         *    import flash.geom.Point;
         * 
         *    import org.libspark.betweenas3.BetweenAS3;
         *    import org.libspark.betweenas3.easing.Cubic;
         *    import org.libspark.betweenas3.tweens.ITween;
         * 
         * 
         *    public class TestMosaic extends Sprite
         *    {
         *        private var _originData:BitmapData = new imgTest(0, 0);        
         *        private var _mosicBit:Bitmap = new Bitmap(new BitmapData(300, 154, false));
         *        private var _num:Object = {pixel:4};
         *        private var _curPixel:int;
         *        
         *        public function TestMosaic()
         *        {
         *            super();
         *            this.addEventListener(Event.ADDED_TO_STAGE, onAdd);
         *        }
         *        
         *        private function onAdd(evt:Event):void
         *        {
         *            evt.currentTarget.removeEventListener(evt.type, arguments.callee);
         *            
         *            _mosicBit.bitmapData.copyPixels(_originData, _originData.rect, new Point());
         *            _curPixel = 4;
         *            _num.pixel = 4;
         *            
         *            var tween:ITween = BetweenAS3.tween(_num, {pixel:50}, null, 2, Cubic.easeInOut);
         *            tween.onUpdate = checkMosaic;
         *            tween.play();
         *            
         *            this.addChild(_mosicBit);
         *        }
         *        
         *        private function checkMosaic():void
         *        {
         *            var pixel:int = int(_num.pixel);
         *            if (_curPixel != pixel) {
         *                _curPixel = pixel;
         *                var bit:BitmapData = Mosaic.draw(_originData, _curPixel);
         *                _mosicBit.bitmapData.copyPixels(bit, _originData.rect, new Point());
         *            }
         *        }
         *    }
         * }
         * </listing> 
		 * 
		 */		
		public static function draw($bit:BitmapData, $size:int):BitmapData
		{
			var half:int = int($size >> 1);
			var pick_x:int;
			var pick_y:int;
            var tot_w:int = $bit.width;
            var tot_h:int = $bit.height;            
            var bit:BitmapData = new BitmapData(tot_w, tot_h, false);
            var color:uint;
            var i:int, k:int;
            for (i=0; i<tot_w; i+=$size) {
				for (k=0; k<tot_h; k+=$size) {
					pick_x = i + half;
					pick_y = k + half;
					if (pick_x >= $bit.width) pick_x = i;
					if (pick_y >= $bit.height) pick_y = k;
					color = $bit.getPixel(pick_x, pick_y);
					bit.fillRect(new Rectangle(i, k, $size, $size), color);
				}
            }            
            return bit;
		}
	}
}